var app = new Vue({
    el: '#app',
    data: {
        host: host,
        username: '',
        old_pwd: '',
        new_pwd: '',
        new_cpwd: '',

        error_opwd: false,
        error_pwd: false,
        error_cpwd: false,
    },
    mounted: function () {
        let u_json = $cookies.get('username')   //使用第三方库获取cookie
        let u_str = JSON.parse(u_json)    //将数据进行json反序列化
        this.username = eval(u_str)     //将unicode编码转为中文
    },

    methods: {
        //退出
        logoutfunc: function () {
            var url = this.host + '/logout/'
            axios.delete(url, {
                responseType: 'json',
                withCredentials: true,
            })
                .then(response => {
                    console.log(response.data)
                    location.href = 'login.html'

                })
                .catch(error => {
                    console.log(error.response)
                })
        },

        //校验旧密码
        check_opwd: function () {
            var re = /^[0-9A-Za-z]{8,20}$/;
            if (re.test(this.old_pwd)) {
                this.error_opwd = false;
            } else {
                this.error_opwd = true;
            }
        },

        //校验新密码
        check_pwd: function () {
            var re = /^[0-9A-Za-z]{8,20}$/;
            if (re.test(this.new_pwd)) {
                this.error_pwd = false;
            } else {
                this.error_pwd = true;
            }
        },

        check_cpwd:function () {
            if(this.new_pwd != this.new_cpwd){
                this.error_cpwd=true;
            }else {
                this.error_cpwd=false;
            }
        },

        //提交
        change_password: function () {
            this.check_opwd();
            this.check_pwd();
            this.check_cpwd();

            if (this.error_pwd == true || this.error_opwd == true || this.error_cpwd == true) {
                window.event.returnValue = false;
                return
            }
            var url = this.host + '/password/';
            axios.put(url,{
                old_pwd: this.old_pwd,
                new_pwd: this.new_pwd,
                new_cpwd: this.new_cpwd,
            },{
                responseType: 'json',
                withCredentials: true,
            })
                .then(response =>{
                    if(response.data.code==0){
                        location.href='login.html'
                    }else {
                        alert(response.data.errmsg)
                    }

            })
                .catch(error =>{

            })

        },

    }
})